import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'history', // 添加这一行
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import('../views/login.vue')
    },
    {
      path: '/index',
      name: 'index',
      component: () => import('../views/index.vue')
    },
    {
      path: '/user/chat',
      name: 'chat',
      component: () => import('../views/user/chat.vue')
    },
    {
      path: '/user/test',
      name: 'test',
      component: () => import('../views/user/test.vue')
    },
    {
      path: '/goods/list',
      name: 'list',
      component: () => import('../views/goods/list.vue')
    },
    {
      path: "/goods/details/:id",
      name: "details",
      component: () => import('../views/goods/details.vue')
    },
    {
      path: "/goods/review",
      name: "review",
      component: () => import('../views/goods/review.vue'),
    },
    {
      path: "/user/list",
      name: "user_list",
      component: () => import('../views/user/list.vue')
    },
    {
      path: "/order/list",
      name: "order_list",
      component: () => import('../views/order/list.vue')
    },
    {
      path: '/order/details/:id',
      name: 'order_details',
      component: () => import('../views/order/details.vue')
    },
    {
      path: '/supplier/list',
      name: 'supplier_list',
      component: () => import('../views/supplier/list.vue')
    },
    {
      path: '/supplier/goods/:id',
      name: 'suppller_goods',
      component: () => import('../views/supplier/supplierList.vue')
    },
    {
      path: '/supplier/order/:id',
      name: 'suppller_order',
      component: () => import('../views/supplier/supplierOrder.vue')
    }
  ]
})

//添加导航守卫
router.beforeEach((to, form, next) => {
  //访问下面路由直接放行
  if (to.path == "/index" || to.path == "/") {
    next()
  } else {
    const admin_token = localStorage.getItem("admin_token")
    // front的需要权限的url
    if (admin_token == "" || admin_token == null || admin_token == '') {
      next("/")
    } else {
      next()
    }
  }
})

export default router
